package com.weiqiao.mall.repository.impl;

import com.mongodb.client.MongoCollection;
import com.weiqiao.base.dao.impl.BaseMongoDaoImpl;
import com.weiqiao.mall.repository.ICommodityRepository;
import com.weiqiao.modules.cache.MyCacheAble;
import com.weiqiao.modules.cache.MyCacheEvict;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("CommodityRepositoryImpl")
public class CommodityRepositoryImpl extends BaseMongoDaoImpl implements ICommodityRepository {

    private String collectionName = "commodity";

    @Override
    public boolean updateOneCommodityByJson(int companyId, Bson filter, String json) {
        Document doc = Document.parse(json);
        return this.updateOne(collectionName, filter, doc, true);
    }

    @Override
    public List<String> findCommoditys(int companyId, Bson filter) {
        return this.findFilter(collectionName, filter);
    }

    @Override
    public String findCommodity(int companyId, Bson filter) {
        return this.findOne(collectionName, filter);
    }

    @Override
    public boolean updateOneCommodity(int companyId, Bson filter, Document doc, boolean boo) {
        return this.updateOne(collectionName, filter, doc, boo);
    }

    @Override
    public int countCommodity(int companyId, Bson bson) {
        MongoCollection<Document> dbCollection = getCollection(collectionName);
        return (int) dbCollection.count(bson);
    }

    @Override
    public String findCommodityById(int companyId, Bson bson) {
        return this.findOne(collectionName, bson);
    }

    @Override
    public boolean updateMany(int companyId, Bson filter, Document doc, boolean b) {
        return this.updateMany(collectionName, filter, doc, false);
    }

    @Override
    public List<String> findCommodityAndLimit(int companyId, Bson filter, int limit) {
        return this.findFilterLimit(collectionName,filter,limit);
    }

    @Override
    public List<String> findCommodityByFilterAndPage(int companyId, Bson filter, Pageable pageable) {
        return super.getListAndPage(collectionName, filter, pageable);
    }

    @Override
    public List<String> findListAndProject(int companyId, Document filter, Document project) {
        return super.findToProject(collectionName, filter, project);
    }

    @Override
    public List<String> findCommoditySortAndLimit(int companyId, Bson filter, Bson filterSort, int limit) {
        return this.findFilterSortLimit(collectionName, filter, filterSort, limit);
    }
}
